package com.rdzn.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.rdzn.mall.center.entity.OmsOrderItem;
import com.rdzn.mall.center.service.IOmsOrderItemService;
import com.rdzn.mall.portal.domain.OrderItem;
import com.rdzn.mall.portal.domain.PmsPortalProductDetail;
import com.rdzn.mall.portal.domain.SkuStock;
import com.rdzn.mall.portal.service.IOrderItemService;
import com.rdzn.mall.portal.service.IProductService;
import com.rdzn.mall.portal.service.ISkuStockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderItemServiceImpl implements IOrderItemService {

    @Autowired
    private IOmsOrderItemService orderItemService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISkuStockService skuStockService;

    @Override
    public List<OrderItem> listByOrderId(Integer orderId) {
        return this.covert(orderItemService.listByOrderId(orderId));
    }

    @Override
    public List<OrderItem> listByOrderIds(List<Integer> orderIds) {
        return covert(orderItemService.listByOrderIds(orderIds));
    }

    private List<OrderItem> covert(List<OmsOrderItem> orderItemList){
        if (CollectionUtils.isEmpty(orderItemList)){
            return Collections.emptyList();
        }
        return orderItemList.stream().map(omsOrderItem -> {
            OrderItem orderItem = new OrderItem();
            BeanUtils.copyProperties(omsOrderItem, orderItem);
            PmsPortalProductDetail productDetail = productService.getDetail(orderItem.getProductId());
            orderItem.setProduct(productDetail);
            SkuStock skuStock = skuStockService.getByProductSkuId(orderItem.getProductSkuId());
            orderItem.setSku(skuStock);
            return orderItem;
        }).collect(Collectors.toList());
    }
}
